package cn.fl.project.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.project.condition.PrjCstAssMbInfoQueryCondition;
import cn.fl.project.dto.PrjCstAssMbInfoDTO;
import cn.fl.project.service.IPrjCstAssMbInfoService;
import cn.fl.project.service.IProjectApiService;
import cn.fl.project.transform.PrjCstAssMbInfoVOTransform;
import cn.fl.project.vo.PrjCstAssMbInfoVO;

/**
 * @Description: 项目客户资产抵押担保信息表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class PrjCstAssMbInfoServiceImpl implements IPrjCstAssMbInfoService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IPrjCstAssMbInfoService.class);
	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private IProjectApiService projectApiService;
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;
	

	@Override
	public PrjCstAssMbInfoVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//TODO 业务逻辑
		PrjCstAssMbInfoDTO dto=this.projectApiService.selectPrjCstAssMbInfoById(id,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
		return PrjCstAssMbInfoVOTransform.toVO(dto);
	}

	@Override
	public Long add(PrjCstAssMbInfoVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		return this.projectApiService.addPrjCstAssMbInfo(PrjCstAssMbInfoVOTransform.toDTO(entity));
	}

	@Override
	public boolean modify(PrjCstAssMbInfoVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"修改对象参数不能为空！");
		}
		return this.projectApiService.modifyPrjCstAssMbInfo(PrjCstAssMbInfoVOTransform.toDTO(entity));
	}
	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.prjCstAssMbInfoApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"请选择要删除的数据！");
		};
		return this.projectApiService.removePrjCstAssMbInfoByIds(ids);
    }



	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		page = this.projectApiService.searchPrjCstAssMbInfoListPage(page, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
		page.setResultList(PrjCstAssMbInfoVOTransform.toVOList((List<PrjCstAssMbInfoDTO>)page.getResultList()));
		return page;
	}

	@Override
	public List<PrjCstAssMbInfoVO> getPrjCstAssMbInfoListByPropCd(String assPropCd) throws BaseException {
		if(StringUtils.isBlank(assPropCd)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"抵押物类型不能为空！");
		}
		return PrjCstAssMbInfoVOTransform.toVOList(this.projectApiService.getPrjCstAssMbInfoListByPropCd(assPropCd,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd()));
	}



	@Override
	public List<KeyValueVO> getAssRlCdList() throws BaseException {
		String code = DictionaryConstant.CODE_PRJ_ASS_RL_CD;
		return getKeyValueList(code);
	}

	@Override
	public List<KeyValueVO> getMStsCdList() throws BaseException {
		String code = DictionaryConstant.CODE_PRJ_M_STS_CD;
		return getKeyValueList(code);
	}

	@Override
	public List<KeyValueVO> getNatureCdList() throws BaseException {
		String code = DictionaryConstant.CODE_PRJ_NATURE_CD;
		return getKeyValueList(code);
	}
	public List<KeyValueVO> getKeyValueList(String code) throws BaseException {
		List<KeyValueVO>  keyValueList = null;
		List<SysDictionaryDTO>  dictionaryList = this.sysDictionaryApiService.searchGroup(CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd(),code);
		if(CollectionUtils.isNotEmpty(dictionaryList)){
			keyValueList = new ArrayList<>();
			for (SysDictionaryDTO sysDictionaryDTO : dictionaryList) {
				KeyValueVO keyValue = new KeyValueVO();
				keyValue.setKey(sysDictionaryDTO.getCode());
				keyValue.setValue(sysDictionaryDTO.getCodeNm());
				keyValueList.add(keyValue);
			}
		}
		return keyValueList;
	}

	@Override
	public List<PrjCstAssMbInfoVO> searchList(PrjCstAssMbInfoQueryCondition assQc,String sysCd,String orgCd) {
		if(null==assQc) return null;
		return PrjCstAssMbInfoVOTransform.toVOList(projectApiService.searchPrjCstAssMbInfoList(assQc,sysCd,orgCd));
	}
}
